package com.breezelin.freshmarket.activities;

/**
 * Created by Breeze Lin
 * 2016/5/19 02:25
 * 1linyufeng1@gmail.com
 */

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.adpaters.PicScanAdapter;
import com.breezelin.freshmarket.adpaters.RelateAdapter;
import com.breezelin.freshmarket.adpaters.SpecAdapter;
import com.breezelin.freshmarket.app.BaseActivity;
import com.breezelin.freshmarket.entities.data.Product;
import com.breezelin.freshmarket.entities.data.ProductDetail;
import com.breezelin.freshmarket.entities.data.Spec;
import com.breezelin.freshmarket.entities.rsp.QueryRsp;
import com.breezelin.freshmarket.network.TaskCanceler;
import com.breezelin.freshmarket.network.asynctasks.AboutQueryTask;
import com.breezelin.freshmarket.network.asynctasks.ProductDetailTask;
import com.breezelin.freshmarket.utils.UmengUtils;

import java.util.LinkedList;

/**
 * 产品详情页面
 */
public class DetailActivity extends BaseActivity implements View.OnClickListener,
		RelateAdapter.RelateClickListener, PicScanAdapter.MomentPicListener, ViewPager.OnPageChangeListener {

	/**
	 * 标题栏
	 */
	private Toolbar toolbar;
	/**
	 * 当前页面的产品
	 */
	private ProductDetail content;
	/**
	 * 规格
	 */
	private LinkedList<Spec> specs;
	/**
	 * 商品类型
	 */
	private String type;
	/**
	 * 商品id
	 */
	private String id;
	/**
	 * 相关产品
	 */
	private LinkedList<Product> products;

	/**
	 * 产品图片展示
	 */
	private ViewPager pager;
	/**
	 * 产品图片翻看计数
	 */
	private TextView imgCountTxt;
	/**
	 * 产品名
	 */
	private TextView nameTxt;
	/**
	 * 产品价格
	 */
	private TextView priceTxt;
	/**
	 * 规格列表
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private RecyclerView specList;
	/**
	 * 规格适配器
	 */
	private SpecAdapter specAdapter;
	/**
	 * 相关产品
	 */
	@SuppressWarnings("FieldCanBeLocal")
	private RecyclerView aboutList;
	/**
	 * 相关产品适配器
	 */
	private RelateAdapter relateAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);

		initData();
		bindViews();
		getData();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		TaskCanceler.cancelDetailRequests();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 单点分享回调
		UmengUtils.ssoCallback(this, requestCode, resultCode, data);
	}

	// 初始化数据
	private void initData() {
		id = getIntent().getStringExtra("id");
		type = getIntent().getStringExtra("type");
		specs = new LinkedList<>();
		products = new LinkedList<>();
	}

	// 绑定视图
	private void bindViews() {
		toolbar = (Toolbar) findViewById(R.id.toolbar);
		// 设置标题栏
		setSupportActionBar(toolbar);
		// 返回键可用
		//noinspection ConstantConditions
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		toolbar.setNavigationOnClickListener(this);
		pager = (ViewPager) findViewById(R.id.pager);
		pager.addOnPageChangeListener(this);
		nameTxt = (TextView) findViewById(R.id.txt_name);
		imgCountTxt = (TextView) findViewById(R.id.txt_img_count);
		priceTxt = (TextView) findViewById(R.id.txt_price);
		specList = (RecyclerView) findViewById(R.id.list_spec);
		specList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
				false));
		specAdapter = new SpecAdapter(this, specs);
		specList.setAdapter(specAdapter);
		aboutList = (RecyclerView) findViewById(R.id.list_about);
		aboutList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,
				false));
		relateAdapter = new RelateAdapter(this, products, this);
		aboutList.setAdapter(relateAdapter);
	}

	//  获取数据
	private void getData() {
		new ProductDetailTask(id) {
			@Override
			protected void onSuccess(ProductDetail productDetail) {
				content = productDetail;
				setData();
			}
		}.execute();
		new AboutQueryTask(id, type) {
			@Override
			protected void onSuccess(QueryRsp queryRsp) {
				products.addAll(queryRsp.getCommodities());
				relateAdapter.notifyDataSetChanged();
			}
		}.execute();
	}

	// 设置数据
	private void setData() {
		toolbar.setTitle(content.getCommodityName());
		nameTxt.setText(content.getCommodityName());
		priceTxt.setText(String.format(getString(R.string.str_price), content.getCommodityPrice()));
		specs.addAll(content.getSpeces());
		specAdapter.notifyDataSetChanged();
		// 图片轮播控件必须每次重设一个适配器
		LinkedList<String> pictures = content.getPictures();
		pager.setAdapter(new PicScanAdapter(this, pictures, this));
		// 图片数目显示
		String text = "1/" + pictures.size();
		imgCountTxt.setText(text);
	}

	/**
	 * 界面分享相关按钮点击回调
	 *
	 * @param view 按钮的视图控件
	 */
	public void share(View view) {
		int media = UmengUtils.SHARE_MEDIA_NONE;
		switch (view.getId()) {
			case R.id.btn_share_weibo:
				media = UmengUtils.SHARE_MEDIA_WEIBO;
				break;
			case R.id.btn_share_wechat:
				media = UmengUtils.SHARE_MEDIA_WECHAT;
				break;
			case R.id.btn_share_circle:
				media = UmengUtils.SHARE_MEDIA_CIRCLE;
				break;
			case R.id.btn_share_qq:
				media = UmengUtils.SHARE_MEDIA_QQ;
				break;
			case R.id.btn_share_qzone:
				media = UmengUtils.SHARE_MEDIA_QZONE;
				break;
			case R.id.btn_share_link:
				media = UmengUtils.SHARE_MEDIA_COPY;
				break;
		}
		UmengUtils.share(this, media, content.getCommodityName(), getString(R.string.share_content),
				content.getPictures().getFirst(), "http://www.baidu.com/");
	}

	@Override
	public void onClick(View v) {
		// 左上返回按钮的点击回调，直接调用返回功能
		onBackPressed();
	}

	@Override
	public void onRelateClick(Product product) {
		// 相关商品点击回调
		Intent intent = new Intent(this, DetailActivity.class);
		intent.putExtra("id", product.getCommodityId());
		startActivity(intent);
	}

	@Override
	public void onPicClick(int position) {
		// 图片轮播点击回调
		showToast("点我干嘛？");
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	}

	@Override
	public void onPageSelected(int position) {
		// 翻页的时候，将当前的页数回调给显示图片进度的控件
		imgCountTxt.setText((position + 1) + "/" + content.getPictures().size());
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}
}
